package com.example.anitamusicspace.controller;

import com.example.anitamusicspace.mapper.LoveMusicMapper;
import com.example.anitamusicspace.model.Music;
import com.example.anitamusicspace.model.User;
import com.example.anitamusicspace.service.LoveMusicService;
import com.example.anitamusicspace.tools.Constant;
import com.example.anitamusicspace.tools.ResponseBodyMessage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 小梅就是酱子
 * Date: 2022-08-05
 * Time: 20:52
 */
@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {
    @Resource
    private LoveMusicService loveMusicService;

    //添加到收藏音乐
    @RequestMapping("/likeMusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id, HttpServletRequest req) {
        int musicId = Integer.parseInt(id);
        System.out.println("likeMusic->musicId: "+musicId);
        //判断是否登录
        HttpSession httpSession = req.getSession(false);
        if(httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录！");
            return new ResponseBodyMessage<>(-1,"没有登录",false);
        }
        User user = (User)httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("likeMusic->userID: "+userId);
        //查询当前用户是否点赞过该音乐
        Music music = loveMusicService.findLoveMusicByMusicIdAndUserId(userId,musicId);
        if(music!=null) {
            //之前收藏过,就不能再收藏了
            return new ResponseBodyMessage<>(-1,"该用户点赞过该音乐",false);
        }else {
            boolean effect = loveMusicService.insertLoveMusic(userId,musicId);
            if(effect) {
                return new ResponseBodyMessage<>(0,"点赞音乐成功",true);
            }else {
                return new ResponseBodyMessage<>(-1,"点赞音乐失败",false);
            }
        }
    }


    //------------------------------------------------------------------------------------------------------------------

        //取消音乐的收藏
        @RequestMapping("/deletelovemusic")
        public ResponseBodyMessage<Boolean> deleteLoveMusic(@RequestParam String id, HttpServletRequest req) {
            int musicId = Integer.parseInt(id);
            //判断是否登录
            HttpSession httpSession = req.getSession(false);
            if(httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
                System.out.println("没有登录！");
                return new ResponseBodyMessage<>(-1,"没有登录",false);
            }
            User user = (User)httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
            int userid = user.getId();
            int ret = loveMusicService.deleteLoveMusic(userid,musicId);
            if(ret == 1) {
                return new ResponseBodyMessage<>(0,"取消收藏成功！",true);
            }else {
                return new ResponseBodyMessage<>(0,"取消收藏失败！",false);
            }
        }

    //------------------------------------------------------------------------------------------------------------------

    //查询收藏了的音乐
    @RequestMapping("/findlovemusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required=false)String musicName,
                                                          HttpServletRequest req) {
        //判断是否登录
        HttpSession httpSession = req.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录！");
            return new ResponseBodyMessage<>(-1, "没有登录", null);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userid = user.getId();
        List<Music> musics = new ArrayList<>();
        if (musicName != null) {
            musics = loveMusicService.findLoveMusicBykeyAndUID(musicName, userid);
        } else {
            musics = loveMusicService.findLoveMusicByUserId(userid);
        }
        return new ResponseBodyMessage<>(0,"查询到了所有的收藏的音乐",musics);
    }



}
